package com.guigu.aoyun.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.aoyun.entity.Tickets;
import com.guigu.aoyun.entity.vo.MyTickets;
import com.guigu.aoyun.entity.vo.TicketsQuery;
import com.guigu.aoyun.service.TicketsService;
import com.guigu.commonutils.R;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@RestController
@RequestMapping("/tickets")
@CrossOrigin
public class TicketsController {

    @Autowired
    private TicketsService ticketsService;


    // 根据编号查询，供前端调用
    @GetMapping("{id}")
    public R getById(@PathVariable Integer id) {
        Tickets tickets = ticketsService.getById(id);
        return R.ok().data("tickets", tickets);
    }

    // 修改
    @PostMapping("/update")
    public R update(@RequestBody Tickets tickets) {
        boolean flag = ticketsService.updateById(tickets);
        if(flag)
            return R.ok();
        return R.error();
    }

    @PostMapping("page/{current}/{size}")
    public R page(
            @PathVariable Long current,
            @PathVariable Long size,
            @RequestBody TicketsQuery ticketsQuery) {
        Page<MyTickets> topicPage = new Page<>(current, size);
        ticketsService.page(topicPage, ticketsQuery);
        return R.ok().data("rows", topicPage.getRecords())
                .data("total", topicPage.getTotal())
                .data("pages", topicPage.getPages())
                .data("current", topicPage.getCurrent())
                .data("size", topicPage.getSize());
    }

    // 添加
    @PostMapping
    public R add(
            @RequestBody Tickets tickets) {
        tickets.setBeginTime(new Date());
        boolean flag = ticketsService.save(tickets);
        if(flag)
            return R.ok();
        return R.error();
    }

    // 删除
    @DeleteMapping("/{id}")
    public R remove(@PathVariable Integer id) {
        boolean flag = ticketsService.removeById(id);
        if(flag)
            return R.ok();
        return R.error();
    }


}
